<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['types']=array(
	'1'=>'普通防伪码',
	'2'=>'分节防伪码',
	'3'=>'防伪二维码',
);
if($_RQ['op']=='list'){
	if($_RQ['id']){
		$goods_id=intval($_RQ['id']);
		$this->session('goods_id',$goods_id);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']);
	if($goods_id){
		$params['goods_id']=$goods_id;
		$return['params']['goods_id']=$goods_id;
	}
	//批号
	if(!empty($_RQ['code'])){
		$params['code']=intval($_RQ['code']);
		$return['params']['code']=$_RQ['code'];
	}
	//备注
	if(!empty($_RQ['remark'])){
		$params['remark']='%'.trim($_RQ['remark']).'%';
		$return['params']['remark']=$_RQ['remark'];
	}
    $fields=array('id','goods_id','goods_code','code','num','xnum','startxn','endxn','startnum','endnum','type','createtime','status','remark');
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_goodserpfw_batch',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		//$item['goods_title']=$item['goods_title']?$item['goods_title']:'无';
		if(!$item['goods_id']){
			$item['is_allgoods']=1;
		}else{
			$item['is_allgoods']=0;
			$item['goods_title']=pdo_getcolumn('jy_goodserpfw_goods',array('id'=>$item['goods_id']),'title');
			$goods_code=pdo_getcolumn('jy_goodserpfw_goods',array('id'=>$item['goods_id']),'code');
			if($goods_code!=$item['goods_code']){
				pdo_update('jy_goodserpfw_batch',array('goods_code'=>$goods_code),array('id'=>$item['id']));
			}
		}
		$item['startxn']=sprintf("%08d", $item['startxn']);
		$item['endxn']=sprintf("%08d", $item['endxn']);
		$item['startnum']=sprintf("%08d", $item['startnum']);
		$item['endnum']=sprintf("%08d", $item['endnum']);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('1'=>'正常','0'=>'失效');		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'批次管理'),
			array('op'=>'add','title'=>'添加批次'),
		),
		'list'=>array(
		'tip'=>'防伪的<em>业务逻辑</em>：</br>
				1、每个产品，生成一批次，防伪码；</br>
				2、找印刷商，每行生成一个二维码，批量印刷二维码（<em>温馨提示</em>：下载的只是字符串/链接地址。印刷商，会帮你转成二维码），如果直接下载二维码，文件太大，没什么意义。；</br>
				3、然后贴到每个产品包装上面去，客户扫这个防伪码，检验是否正品！',
		'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'code','title'=>'批号','type'=>'string'),
			array('name'=>'is_allgoods','title'=>'所属','type'=>'radio','options'=>'is_allgoods'),
			array('name'=>'goods_title','title'=>'赋码商品','type'=>'string'),
			array('name'=>'xnum','title'=>'箱标签数量','type'=>'string'),
			array('name'=>'startxn','title'=>'箱标签开始','type'=>'string'),
			array('name'=>'endxn','title'=>'箱标签结尾','type'=>'string'),
			array('name'=>'num','title'=>'小标签数量','type'=>'string'),
			//array('name'=>'startnum','title'=>'小标签开始','type'=>'string'),
			//array('name'=>'endnum','title'=>'小标签结尾','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		'action'=>array(
			array('type'=>'post','op'=>'down_xcode','title'=>'下载箱标签'),
			array('type'=>'modal','op'=>'show_xcode','title'=>'查看示例箱标签'),
			array('type'=>'post','op'=>'down_code','title'=>'下载小标签'),
			array('type'=>'modal','op'=>'show_code','title'=>'查看示例小标签'),
			array('type'=>'modal','op'=>'delset','title'=>'删除批次'),
		)
		),
		'search'=>array(			
			array('name'=>'goods_id','width'=>15,'title'=>'所属商品ID','type'=>'choose','op'=>'choose_goods'),
			array('name'=>'code','width'=>15,'title'=>'批号','type'=>'string'),
			array('name'=>'remark','width'=>15,'title'=>'备注','type'=>'string'),
		)
	);
	$return['is_allgoods']=array('单商品','全商品');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$num=intval($postdata['beinum']);
		if(!$num){
			exi('请先设置每箱拖数！','error');
		}
		if(!$postdata['xnum']){
			exi('请先设置生成箱标签数量！','error');
		}
		if($postdata['xnum']*$num>5000){
			exi('同批次最多只能生成5000个盒码！您可以分多批生成！','error');
		}
		$type=intval($postdata['type']);
		if(!$type){
			exi('请先设置防伪码类型！','error');
		}
		$goods_id=intval($postdata['goods_id']);
		if(!$goods_id && !$postdata['is_allgoods']){
			exi('请先选择商品！','error');
		}
		$return=$this->model('batch')->add_batch($goods_id,$postdata['beinum'],$type,$postdata['remark'],0,$postdata['xnum']);
		if($return){
			exi('防伪码生成成功！请前往列表页下载！','','/batch/list');
		}else{
			exi('添加失败','error','/batch/list');
		}
	
	}	
	$item=array('type'=>3,'goods_id'=>$this->session('goods_id'),'is_allgoods'=>0);
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'',
		'desc'=>'模板设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'批次管理'),
			array('op'=>'edit','title'=>'添加批次')
		),
		'edit'=>array(
			array('name'=>'is_allgoods','title'=>'全商品防伪？','type'=>'switch','help'=>'开启后，生成防伪码无需绑定商品，全商品都用一批次防伪码'),
			array('type'=>'choose','name'=>'goods_id','title'=>'所属商品','op'=>'choose_goods','show'=>array('is_allgoods'=>0),'help'=>'要先前往【赋码管理】-为商品赋码！'),
			array('name'=>'xnum','title'=>'箱标签数量','type'=>'string'),
			array('name'=>'beinum','title'=>'每箱拖数','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types','help'=>'普通防伪码：只是一串编号字符，防伪二维码：是带链接的地址。'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='down_code'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择批次','error','list');	
	}
	$file=pdo_getcolumn('jy_goodserpfw_batch',array('id'=>$id,'merchid'=>$_SESSION['onemerchid']),'code_file');
	$file=tomedia($file);
	exi('','','',$file);
}
if($_RQ['op']=='show_code'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择批次！','error');
	}
	$item=pdo_get('jy_goodserpfw_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),array('show_code'));
	if(!$item){
		exi('指定批次不存在！','error');
	}
	$return['item']=array(
		'code'=>$item['show_code'],
		'format_code'=>$this->format($item['show_code']),
		'ewm_code'=>custom_url('jy_goodserpfw/search/main/code='.$item['show_code'],true),
	);
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'扫二维码体验',
		'look'=>array(
			array('type'=>'tip','name'=>'code','title'=>'示例防伪码，注意下面的防伪码只做演示之用，实际使用请前往列表页下载。下载下来的只是防伪码，找印刷商，印出二维码。进到《查询入口》==》点扫码'),
			array('type'=>'string','name'=>'code','title'=>'原始防伪码'),
			array('type'=>'string','name'=>'format_code','title'=>'分节防伪码'),
			array('type'=>'ewm','name'=>'ewm_code','title'=>'防伪二维码'),
		),
	);
	exi($return);
}
if($_RQ['op']=='down_xcode'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择批次','error','list');	
	}
	$file=pdo_getcolumn('jy_goodserpfw_batch',array('id'=>$id,'merchid'=>$_SESSION['onemerchid']),'xcode_file');
	$file=tomedia($file);
	exi('','','',$file);
}
if($_RQ['op']=='show_xcode'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择批次！','error');
	}
	$item=pdo_get('jy_goodserpfw_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),array('show_xcode'));
	if(!$item){
		exi('指定批次不存在！','error');
	}
	$return['item']=array(
		'code'=>$item['show_xcode'],
		'format_code'=>$this->format($item['show_xcode']),
		'ewm_code'=>custom_url('jy_goodserpfw/search/main/xcode='.$item['show_xcode'],true),
	);
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'扫二维码体验',
		'look'=>array(
			array('type'=>'tip','name'=>'code','title'=>'示例防伪码，注意下面的防伪码只做演示之用，实际使用请前往列表页下载。下载下来的只是防伪码，找印刷商，印出二维码。进到《查询入口》==》点扫码'),
			array('type'=>'string','name'=>'code','title'=>'原始防伪码'),
			array('type'=>'string','name'=>'format_code','title'=>'分节防伪码'),
			array('type'=>'ewm','name'=>'ewm_code','title'=>'防伪二维码'),
		),
	);
	exi($return);
}
if($_RQ['op']=='delset'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$num=intval();
		$basic=$this->plugin_setting('basic');
		if(!$basic['mima']){
			exi('请先设置【操作密码】（基本设置--防伪码生成设置）！','error');
		}
		if(!$postdata['mima']){
			exi('请先输入【操作密码】！','error');
		}
		if($postdata['mima']!=$basic['mima'].'123'){
			exi('输入的【操作密码】不对！','error');
		}
		$id=$postdata['id'];
		pdo_delete('jy_goodserpfw_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		pdo_delete('jy_goodserpfw_xcode',array('batch_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		pdo_delete('jy_goodserpfw_code',array('batch_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('删除成功！','error','/batch/list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择批次！','error');
	}
	$item=pdo_get('jy_goodserpfw_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'',
		'title'=>'删除批次',
		'edit'=>array(
			array('title'=>'【温馨提示】：本操作会同时删除本批次的所有防伪码标签','type'=>'tip'),
			array('name'=>'mima','title'=>'操作密码','type'=>'string','help'=>'为了安全，必须输入操作密码，才能删除'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	exi('请先输入【操作密码】！','error');
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_goodserpfw_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
		pdo_delete('jy_goodserpfw_xcode',array('batch_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		pdo_delete('jy_goodserpfw_code',array('batch_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_goodserpfw_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				pdo_delete('jy_goodserpfw_xcode',array('batch_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				pdo_delete('jy_goodserpfw_code',array('batch_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='piciaddcode'){
	$batch_id=intval($_RQ['batch_id']);
	if(!$batch_id){
		exi('请先选择批次','error','list');	
	}
	$startnum=intval($_RQ['startnum']);
	$endnum=intval($_RQ['endnum']);
	$batch=pdo_get('jy_goodserpfw_batch',array('id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']));
	$this->model('batch')->addcode($batch,$batch_id,0,$startnum,$endnum,'',1);
	exi('操作完成');
}
if($_RQ['op']=='choose_goods'){
	//选择商品
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	//商品名称
	if(!empty($_RQ['title'])){
		$params['title']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title');
	$list=pdo_getslice('jy_goodserpfw_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('type'=>'string','name'=>'id','title'=>'商品ID'),
			array('type'=>'string','name'=>'title','title'=>'商品名称'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'id','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	exi($return);
}
?>